import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'global_config/constants.dart';
//
import 'layout/nav_bar.dart';
import 'layout/responsive_layout.dart';
import 'layout/side_bar.dart';
//
import 'views/dash_board.dart';
import 'views/documentation.dart';
import 'views/home.dart';
import 'views/login.dart';

void main(List<String> args) {
  runApp(
    FlutterElementAdmin(),
  );
}

class FlutterElementAdmin extends StatelessWidget {
  const FlutterElementAdmin({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      navigatorKey: globalNavigatorKey,
      home: Login(),
      routes: {
        '\"/\"': (BuildContext context) => Login(),
        '/Home': (BuildContext context) => Home(),
        '/DashBoard': (BuildContext context) => DashBoard(),
        '/Documentation': (BuildContext context) => Documentation(),
      },
      builder: (BuildContext context, Widget? child) {
        if (currentMenuPath != '/Login') {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            home: Layout(child: child),
          );
        }
        return child!;
      },
      navigatorObservers: [MyObserver()],
      initialRoute: '/',
    );
  }
}

class MyObserver extends NavigatorObserver {
  @override
  void didPush(Route route, Route? previousRoute) {
    if (kDebugMode) {
      var buttonPath =
          buttons.map((RouteItem buttonsInfo) => buttonsInfo.path).toList();

      if (buttonPath.contains(route.settings.name!)) {
        currentMenuPath = route.settings.name!;
        currentMenuIndex = buttonPath.indexOf(route.settings.name!);
      }
      print(
          'route.settings.name ${route.settings.name} currentMenuIndex $currentMenuIndex');
    }
    super.didPush(route, previousRoute);
  }
}

class Layout extends StatefulWidget {
  const Layout({
    Key? key,
    required this.child,
  }) : super(key: key);

  final Widget? child;
  @override
  State<Layout> createState() => _LayoutState();
}

class _LayoutState extends State<Layout> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size(double.infinity, defaultPadding * 2 + 50),
        child: (ResponsiveLayout.isTinyLimit(context) ||
                ResponsiveLayout.isTinyHeightLimit(context)
            ? Container()
            : NavBar()),
      ),
      body: ResponsiveLayout(
        tiny: Text('tiny'),
        phone: widget.child!,
        tablet: widget.child!,
        largeTablet: widget.child!,
        computer: Row(
          children: [
            SideBar(),
            Expanded(child: widget.child!),
          ],
        ),
      ),
      drawer: SideBar(),
      drawerEnableOpenDragGesture:
          ResponsiveLayout.isComputer(context) ? false : true,
    );
  }
}
